package com.lm.liaoming.location;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.lm.liaoming.constant.ConstantValues;
import com.lm.liaoming.tool.Utils;
import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;

public class CustomTencentLocationTool implements TencentLocationListener {
    private TencentLocationManager mLocationManager;
    private String mRequestParams;
    private Handler uiHandler;
    public CustomTencentLocationTool(Context context, Handler handler) {
        this.uiHandler = handler;
        mLocationManager = TencentLocationManager.getInstance(context);
        // 设置坐标系为 gcj-02, 缺省坐标为 gcj-02, 所以通常不必进行如下调用
        // 20190713-为适应长庆油田数据，坐标系改为WGS84坐标系下的定位
        mLocationManager.setCoordinateType(TencentLocationManager.COORDINATE_TYPE_WGS84);
    }

    @Override
    public void onLocationChanged(TencentLocation location, int error, String reason) {
        if (error == TencentLocation.ERROR_OK) {
            // 将位置信息发送到地图主界面
            Message msg = Message.obtain();
            msg.what = ConstantValues.GPS_LON_LAT_WHAT_VALUE;
            msg.obj = location;
            uiHandler.sendMessage(msg);
            // 定位成功
            StringBuilder sb = new StringBuilder();
            sb.append("定位参数=").append(mRequestParams).append("\n");
            sb.append("(纬度=").append(location.getLatitude()).append(",经度=")
                    .append(location.getLongitude()).append(",精度=")
                    .append(location.getAccuracy()).append("), 来源=")
                    .append(location.getProvider()).append(", 地址=")
                    .append(location.getAddress());
            Utils.i("tencentGCJ02 onLocationChanged 定位成功 :" + sb);
            Utils.i("onLocationChanged, location :" + location.toString());
        } else {
            Utils.i("tencentGCJ02 onLocationChanged 定位失败 :" + reason);
        }
    }

    @Override
    public void onStatusUpdate(String s, int i, String s1) {
        Utils.i("tencentWGS84 onStatusUpdate  :" + s + " , " + i + ", " + s1);
    }
    public void startLocation() {
        TencentLocationRequest request = TencentLocationRequest.create();
        request.setInterval(2000);
        request.setAllowCache(false);
        request.setAllowDirection(true);
        request.setAllowGPS(true);
        request.setQQ("826584661");
        request.setRequestLevel(TencentLocationRequest.REQUEST_LEVEL_ADMIN_AREA);

        mLocationManager.requestLocationUpdates(request, this);

        mRequestParams = request.toString() + ", 坐标系="
                + DemoUtils.toString(mLocationManager.getCoordinateType());
    }
    // 退出activity前务必要停止定位
    public void stopLocation() {
        mLocationManager.removeUpdates(this);
    }
}
